/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.HashMap;
import java.util.Map;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.ICountersResult;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 counters' command.
  * 
  * @version $Revision: 16 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Counters extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the result of the operation. */
	private Result result = new Result();

	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of Counters.
	  * 
	  * @return the Counters object.
	  */
	public static final Counters newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);
		return new Counters(status);
	}
	
	// ------------------------------------------------------------------------ Private Constructors
	
	/** Constructor for Counters.
	  * 
	  * @param status the MultiStatus.
	  */
	private Counters(MultiStatus status)
	{
		super(status);
	}

	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see IClientUserDelegate#outputStat(Map) */
	public void outputStat(Map strDict)
	{
		String key = (String) strDict.get("counter");                                  //$NON-NLS-1$
		Integer value = Helper.safeGetInteger(strDict, "value");                       //$NON-NLS-1$
		
		result.putCounterValue(key, value);
	}

	// ----------------------------------------------------------- Public Methods (IPerforceCommand)

	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "counters";                                                             //$NON-NLS-1$
	}
	
	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return getMultiStatus();
	}
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return true;
	}	
	
	// ------------------------------------------------------------------------------ Public Methods
	
	/** Returns the result of the operation.
	  * 
	  * @return the result object.
	  */
	public ICountersResult getResult()
	{
		return result;
	}
	
	// ---------------------------------------------------------------------- Private Nested Classes
	
	/**
	  * Implementation of the ICountersResult. Stores information provided
	  * by the 'p4 counters' command.
	  * 
	  * @see ICountersResult
	  */
	private static class Result implements ICountersResult
	{
		// ----------------------------------------------------------------------- Private Variables
		
		/** Stores the mapping of counter name to its value. */
		private Map counterValueMap = new HashMap();

		// -------------------------------------------------------------------------- Public Methods
		
		/** @see ICountersResult#getCounterValue(String) */
		public Integer getCounterValue(String counterName)
		{
			return (Integer) counterValueMap.get(counterName);
		}

		// --------------------------------------------------------------------------------- Methods
		
		/** Call this method to associated the given counter name with the specified value.
		  * 
		  * @param counterName the name of the counter.
		  * @param value the value.
		  */
		void putCounterValue(String counterName, Integer value)
		{
			counterValueMap.put(counterName, value);
		}
	}
}
